<template>
  <div style="width: 100%;height: 100vh;background: #53a8ff; overflow: hidden">
    <div style="width: 300px;margin: 150px auto">
      <div style="font-size: 30px;color: black;text-align: center; padding: 30px 0">欢迎注册</div>

      <el-form ref="form" :rules="rules" :model="form" size="normal">
        <!--用户名-->
        <el-form-item prop="name">
          <el-input placeholder="用户名"  prefix-icon="el-icon-user-solid" v-model="form.name"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input placeholder="密码" prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
        </el-form-item>
        <!--确认密码-->
        <el-form-item prop="confirm">
          <el-input placeholder="确认密码" prefix-icon="el-icon-lock" v-model="form.confirm" show-password></el-input>
        </el-form-item>
        <!--注册按钮-->
        <el-form-item style="text-align: center">
          <el-button  style="width: 40%; font-weight: bold" @click="register">注册</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import {isEmpty} from "element-plus/es/utils/util";


export default {
  name: "Register",
  data(){
    return {
      form: {},
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          // {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      }
    }
  },

  methods: {
    register(){
      if (isEmpty(this.form.name)){
        this.$message({
          type: 'error',
          message: '请输入用户名'
        })
        return
      }else if(isEmpty(this.form.password)){
        this.$message({
          type: 'error',
          message: '请输入密码'
        })
        return
      }else if (this.form.password !== this.form.confirm){
        this.$message({
          type: 'error',
          message: '确认密码不一致'
        })
        return
      }

      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/api/user/register", this.form).then(res => {
            if(res.status === '0'){
              this.$message({
                type: "success",
                message: "注册成功!"
              })
              this.$router.push("/login")  //注册成功后页面跳转
            }else {
              this.$message({
                type: "error",
                message: res.message
              })
            }
          })
        }
      })


    }
  }

}
</script>

<style scoped>

</style>
